package net.lainiao.morethread.Chapter5.demo2;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/6.
 */
public class Main {
    public static void main(String[] args) {
        DocumentMock mock = new DocumentMock();
        String[][] document = mock.generateDocument(100, 1000, "the");
        DocumentTask task = new DocumentTask(document, 0, 100, "the");
        ForkJoinPool pool = new ForkJoinPool();
        pool.execute(task);
        do {
            System.out.println("=======================================");
            System.out.println("main: parallelism " + pool.getParallelism());
            System.out.println("main: active threads " + pool.getActiveThreadCount());
            System.out.println("main: task count " + pool.getQueuedTaskCount());
            System.out.println("main: steal count " + pool.getStealCount());
            System.out.println("=======================================");
            try {
                TimeUnit.MILLISECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (!task.isDone());
        pool.shutdown();

        try {
            System.out.println("Main: The word apears " + task.get() + " in the document");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


    }
}

